package com.tuo51.evaluate.repositories.impl;

import com.starinside.wp.repository.BaseNativeSqlRepository;
import com.tuo51.evaluate.repositories.AnswerNativeRepository;
import com.tuo51.evaluate.vo.evaluate.EvaluateV2AnswerVO;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@CacheConfig(cacheNames = "AnswerNativeRepositoryImpl")
public class AnswerNativeRepositoryImpl extends BaseNativeSqlRepository implements AnswerNativeRepository {
    @Override
    @Cacheable(keyGenerator = "keyGenerator")
    public List<EvaluateV2AnswerVO> findAnswerSheet(String recordId) {

        // TODO: 这个查询需要优化，改查询或者改java代码，二选一

        String querySql = "SELECT " +
                "     i.dimensionName,a.month, q.id as qid, q.indexOnPaper, a.title,a.checked " +
                "FROM " +
                "    tb_evaluate_record_answer a " +
                "    left join tb_evaluate_question q on a.questionId = q.id " +
                "    left join tb_evaluate_item i on q.itemId = i.id " +
                "    left join `tb_evaluate_dimension`  d on d.`id` = i.`dimensionId` " +
                "WHERE " +
                String.format("    a.recordId = '%s' ", recordId ) +
                "ORDER BY d.`code`, q.`indexOnPaper`, a.created ";

        List<EvaluateV2AnswerVO> objectArrayList = sqlObjectList(querySql, EvaluateV2AnswerVO.class);

        return objectArrayList;
    }
}
